package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.queryBean.BankQueryBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.TreeBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.BusinessAreaMapper;
import com.koron.css2.serviceManage.queryBean.BusinessAreaQueryBean;
import com.koron.css2.serviceManage.vo.BusinessAreaVo;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

public class BusinessAreaTree implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        // TODO Auto-generated method stub
        MessageBean<TreeBean> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", TreeBean.class);
        try {
            if (req.getData() == null || "".equals(req.getData())) {
                req.setData(new BankQueryBean());
            }
            BusinessAreaMapper mapper = factory.getMapper(BusinessAreaMapper.class);
            BusinessAreaQueryBean selectBean = JsonUtils.objectToPojo(req.getData(), BusinessAreaQueryBean.class);

            selectBean.setGroupCode(userInfo.getCurWaterCode());
            selectBean.setStatus(1);
            List<BusinessAreaVo> list = mapper.selectBusinessAreaList(selectBean);

            // TODO 是否只查询启用状态的银行信息
//	            selectBean.setIsLeaf(0);//只查询目录
            TreeBean treeBean = new TreeBean();
            // 创建根目录
            treeBean.setId("0");
            treeBean.setCode("");
            treeBean.setName("根目录");
            treeBean.setParent("");
            treeBean.setIsParent(true);
            // 递归下级目录
            recTree(list, treeBean);
            info.setCode(Constant.MESSAGE_INT_SUCCESS);
            info.setDescription("success");
            info.setData(treeBean);
        } catch (Exception e) {
            info.setCode(Constant.MESSAGE_DBFAIL);
            info.setDescription("数据库异常");
            logger.error("数据库异常", e);
            factory.close(false);
        }
        return info;
    }
    /**
     * 递归查询下级目录树
     *
     * @param list
     * @param treeBean
     */
    private void recTree(List<BusinessAreaVo> list, TreeBean treeBean) {
        for (BusinessAreaVo bean : list) {
            // 如果parentId=父级id，，说明这是他的下级
            if (treeBean.getId().equals(bean.getParentId())) {
                TreeBean b = new TreeBean();
                b.setId(bean.getId() + "");
                b.setCode(bean.getAreaNo());
                b.setParent(treeBean.getId());
                b.setName(bean.getAreaName());
				b.setCreateName(bean.getCreateName());
				b.setCreateTime(bean.getCreateTime());
                b.setIsParent(false);
                treeBean.setIsParent(true);
                treeBean.getChildren().add(b);
                recTree(list, b); // 递归循环下级目录
            }
        }
    }
}
